#-------------------------------------------------------------------------------
# Name:        模块1
# Purpose:
#
# Author:      WangJing
#
# Created:     23/02/2024
# Copyright:   (c) WangJing 2024
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import requests
import time
import threading
import mode3
import modifydev
import json
import random
from ultralytics.utils.plotting import Annotator, colors, save_one_box
import torch
import yoloimg
import csv
import os

def course(change,start_time,count,machine):
    total=0
    current_directory = os.getcwd()
    csv_filename = 'E:/auto-myt/data.csv'
    print(csv_filename)
    data = []
    if os.path.exists(csv_filename) and os.path.getsize(csv_filename) > 0:
        with open(csv_filename, newline='') as csvfile:
            reader = csv.reader(csvfile)
            data = list(reader)

    for d in range(0, count):
        threads = []
        for i in range(machine):
            print(i,data[i][0])

         #if(i==0):
            t = threading.Thread(target=mode3.fun(data[i][0],i,change).do)
            threads.append(t)
            t.start()

            print('threads len=',len(threads))

        for t in threads:
            t.join()

        total+=8

        if(i%2==0):
            end_time = time.time()
            elapsed_time = end_time - start_time
            print(f"{i}程序运行时间：{elapsed_time} 秒  total===={total}")

    return total


def st(weights=None,count=1,machine=1):
    change=modifydev.change()
    total=0
    start_time = time.time()

    yoloimg.Singleton.register_class('MyModel', yoloimg.MyModel(weights))

    yoloimg.my_model_instance = yoloimg.Singleton.get_class_instance('MyModel')
    yoloimg.model=yoloimg.my_model_instance.class_instance.model()

    change=modifydev.change()

    total=course(change,start_time,count,machine)

    end_time = time.time()

    # 计算并打印程序的运行时间
    elapsed_time = end_time - start_time
    print(f"程序总运行时间：{elapsed_time} 秒 total={total}")
